package com.cstor.liang.service;

import com.cstor.liang.mapper.PlaceMapper;
import com.cstor.liang.pojo.entity.PlaceEntity;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by pomer on 2017/10/17.
 */
@Service
@Transactional
@CacheConfig(cacheNames = "place")
public class PlaceService extends BaseService<PlaceEntity> {

    @Autowired
    private PlaceMapper mapper;

    /**
     * 地方查询，多条件筛选
     * @param isTop 是否为顶级“地方”
     * @param parentNodeId 父节点的id
     * @param type 地方类型
     * @param placeDesc 地方描述
     * @param adminUserId 地方管理员id
     * @param maintainUserId 地方负责人id
     * @param pageIndex 页码
     * @param pageSize 数量
     * @return
     */
//    @Cacheable
    public List<PlaceEntity> filter(Integer isTop, Integer parentNodeId, Integer type, String placeDesc,
                                    Integer adminUserId, Integer maintainUserId, Integer pageIndex, Integer pageSize){
        Example example = new Example(PlaceEntity.class);
        Example.Criteria criteria = example.createCriteria();

        if (null != pageIndex && null != pageSize)
            PageHelper.startPage(pageIndex, pageSize);
        if (null != isTop)
            criteria.andEqualTo("isTop", isTop);
        if (null != parentNodeId)
            criteria.andEqualTo("parentNodeId", parentNodeId);
        if (null != type)
            criteria.andEqualTo("type", type);
        if (null != placeDesc)
            criteria.andLike("placeDesc", "%" + placeDesc + "%");
        if (null != adminUserId)
            criteria.andEqualTo("adminUserId", adminUserId);
        if (null != maintainUserId)
            criteria.andEqualTo("maintainUserId", maintainUserId);

        return mapper.selectByExample(example);
    }

//    @CacheEvict(allEntries=true)
    public void addPlace(PlaceEntity entity){
        if (null != entity.getId())
            entity.setId(null);

        super.insert(entity);
    }

//    @CacheEvict(allEntries=true)
    public void updatePlace(PlaceEntity now){
        if (null == now.getId())
            throw new RuntimeException("缺少id");

        PlaceEntity origin = super.getById(now.getId());
        if (null != now.getIsTop())
            origin.setIsTop(now.getIsTop());
        if (null != now.getParentNodeId())
            origin.setParentNodeId(now.getParentNodeId());
        if (null != now.getType())
            origin.setType(now.getType());
        if (null != now.getPlaceDesc())
            origin.setPlaceDesc(now.getPlaceDesc());
        if (null != now.getAdminUserId())
            origin.setAdminUserId(now.getAdminUserId());
        if (null != now.getMaintainUserId())
            origin.setMaintainUserId(now.getMaintainUserId());
        super.updateByPrimaryKey(origin);
    }

//    @CacheEvict(allEntries=true)
    public void deletePlace(Integer placeId){
        super.deleteByPrimaryKey(placeId);
    }
}